﻿using System;
using System.Collections.Generic;
using Fabrication.Extensions;

namespace Fabrication
{
    public static class MappedPropertySelectorExtensions
    {
        public static IBuilderOptions<T> Using<T, TSource, TTarget>(this MappedPropertySelector<T, TSource, TTarget> mappedPropertySelector, IDictionary<TSource, TTarget> mapping)
        {
            var sourcePropertyName = mappedPropertySelector.SourceMember.GetPropertyName();
            var targetPropertyName = mappedPropertySelector.TargetPropertySelector.PropertyName;

            var builderOptions = mappedPropertySelector.TargetPropertySelector.BuilderOptions;
            builderOptions.AddMap(typeof(T), sourcePropertyName, targetPropertyName, mapping);
            return builderOptions;
        }

        public static IBuilderOptions<T> Using<T, TSource, TTarget>(this MappedPropertySelector<T, TSource, TTarget> mappedPropertySelector, Func<TSource, TTarget> mapping)
        {
            var sourcePropertyName = mappedPropertySelector.SourceMember.GetPropertyName();
            var targetPropertyName = mappedPropertySelector.TargetPropertySelector.PropertyName;

            var builderOptions = mappedPropertySelector.TargetPropertySelector.BuilderOptions;
            builderOptions.AddMap(typeof(T), sourcePropertyName, targetPropertyName, mapping);
            return builderOptions;
        }

        public static IBuilderOptions<T> Using<T, TSource1, TSource2, TTarget>(this DoubleMappedPropertySelector<T, TSource1, TSource2, TTarget> mappedPropertySelector, Func<TSource1, TSource2, TTarget> mapping)
        {
            var sourcePropertyName1 = mappedPropertySelector.SourceMember1.GetPropertyName();
            var sourcePropertyName2 = mappedPropertySelector.SourceMember2.GetPropertyName();
            var targetPropertyName = mappedPropertySelector.TargetPropertySelector.PropertyName;

            var builderOptions = mappedPropertySelector.TargetPropertySelector.BuilderOptions;
            builderOptions.AddMap(typeof(T), sourcePropertyName1, sourcePropertyName2, targetPropertyName, mapping);
            return builderOptions;
        }
    }
}